<?php

namespace app\wx\controller;

use ClassesWithParents\D;
use model\InsurantModel;
use model\PolicyModel;
use model\UserInfoModel;
use think\Db;
use think\Exception;
use think\Request;
use library\SmsDemo;
/**
 * Desc: 被保险人页面
 * Class Insurant
 * Created By Doyi
 * Date: 2018/10/29
 */
class Insurant extends CommonController
{
    const JOIN_PRICE = 1;               // 参与竞价
    const LOOK_GOODS_INFO = 2;          // 看货信息
    const BOL_INFO = 3;                 // 提货信息
    const RECEIVE_MONEY = 4;            // 收款信息
    const AUTH_REPORT = 5;              // 递交授权书

    const NO_COMMIT = 1;                // 未提交
    const NO_CHECK = 2;                 // 待审核
    const RETURN_CHANGE = 3;            // 驳回修改
    const READY = 4;                    // 已完成

    public $request;

    /**
     * 根据模块分配字段
     * @var array
     */
    public $modules = [
        self::JOIN_PRICE =>[
            'is_join',
            'is_promise',
            'back_price',
        ],
        self::LOOK_GOODS_INFO =>[
            'look_type',
            'address',
            'address_detail',
            'linkman',
            'phone',
            'second_phone',
        ],
        self::BOL_INFO =>[
            'is_pay',
            'pay_result',
            'bol_info',
            'bol_address',
            'bol_address_detail',
            'bol_linkman',
            'bol_phone',
            'bol_days',
        ],
        self::RECEIVE_MONEY =>[
            'pay_name',
            'bank_name',
            'card_num',
            'tax_num',
            'company_address',
        ],
        self::AUTH_REPORT =>[
            'entrust_url',
        ],
    ];


    public $type = [
         '预约联系' => 1,
         '随时看货' => 2,
         '交保证金后看货' =>3
    ];


    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->request = $request->param();
    }

    /**
     * 核对资料列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function protectList()
    {
        $where = [
            'insurant.delete_ts' => 0,
            'insurant.status' => 0,
            'policy.policy_status' => 5,
            'policy.remer_id' => $this->uid,
        ];
        if(isset($this->request['report_num']) && !empty($this->request['report_num'])){
            $where['policy.report_num'] = $this->request['report_num'];
        }
        if(isset($this->request['status']) && !empty($this->request['status'])){
            $where['insurant.status'] = $this->request['status'];
        }
        $res = Db::table('insurant')->where($where)->join('policy','policy.p_id=insurant.p_id','left'
        )->field('policy.p_id,policy.created_at,policy.report_num,insurant.status,policy.policy_num')->select();

        return $this->fetch('insurant/protectList',['res'=>$res]);
    }

    /**
     * 对不同状态进行页面跳转
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_status()
    {
        $policy = $this->request['policy_num'];
        $res = Db::table('policy')->where('policy_num',$policy)->find();
        $insurant = Db::table('insurant')->where('p_id',$res['p_id'])->find();
        if($insurant['status'] == 0){
            $this->redirect('insurant/protect_detail',['policy_num'=>$res['policy_num']]);
        }else if($insurant['status'] == 1){
            $this->redirect('insurant/no_accept',['policy_num'=>$res['policy_num']]);
        }else if($insurant['status'] == 2){
            $this->redirect('insurant/no_accept',['policy_num'=>$res['policy_num']]);
        }else if($insurant['status'] == 3){
            $this->redirect('insurant/returnEdit',['policy_num'=>$res['policy_num']]);
        }else if($insurant['status'] == 4){
            $this->redirect('insurant/no_accept',['policy_num'=>$res['policy_num']]);
        }
    }

    /**
     * 资料未确认详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function protect_detail()
    {
        $policy_num = $this->request['policy_num'];
        $PolicyModel = new PolicyModel();
        $info = $PolicyModel->getPolicyInfo($policy_num)->toArray();
        $info['other_report'] = json_decode($info['other_report']);
        $info['present_img'] = json_decode($info['present_img']);
        $info['present_video'] = json_decode($info['present_video']);
        $info['insurance_type_name'] = $this->getInsurantType($info['insurance_type']);
        $info['goods_type_name'] = $this->getGoodType($info['goods_type']);
        return $this->fetch('insurant/protect_detail',['info'=>$info]);
    }


    /**
     * 核对资料未确认 进行修改状态变为编辑
     * @return false|string
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function changeStatus()
    {
        $policy = $this->request['policy_num'];
        $res = Db::table('policy')->where('policy_num',$policy)->find();
        $insurant = Db::table('insurant')->where('p_id',$res['p_id'])->find();
        if($insurant['status'] == 0){
            $data = [
                'status'=>1,
                'update_at'=>date('Y-m-d H:i:s',time())
            ];
            $res = Db::table('insurant')->where('p_id',$res['p_id'])->update($data);
            if($res){
                return json(['code'=>200,'msg'=> 'success','data'=>$policy]);
            }else{
                return json(['code'=>400,'msg'=> 'fail']);
            }
        }
    }




    /**
     * 历史资料列表
     * @return \think\response\View
     */
    public function historyInfo()
    {
        $where['insurant.ui_id'] = $this->uid;
        $where['insurant.status'] = ['not in',0];
        if(isset($this->request['status']) && !empty($this->request['status'])){

            if(is_int($this->request['status'])){
                $where['insurant.status'] = $this->request['status'];
                if($where['insurant.status'] == 1){
                    $name = '未递交审核';
                }else if($where['insurant.status'] == 2){
                    $name = '待审核';
                }else if($where['insurant.status'] == 3){
                    $name = '驳回';
                }else if($where['insurant.status'] == 4){
                    $name = '已完成';
                }else if($where['insurant.status'] == 0){
                    $where['insurant.status'] = ['not in',0];
                }
                $all = 1;
                $this->assign('name',$name);
                $this->assign('status', $this->request['status']);
            }else{
                $name = $this->request['status'];
                if($this->request['status'] == '未递交审核'){
                    $where['insurant.status'] = 1;
                    $status = 1;
                }else if($this->request['status'] == '待审核'){
                    $where['insurant.status'] = 2;
                    $status = 2;
                }else if($this->request['status'] == '驳回'){
                    $where['insurant.status'] = 3;
                    $status = 3;
                }else if($this->request['status'] == '已完成'){
                    $where['insurant.status'] = 4;
                    $status = 4;
                }else if($this->request['status'] == '全部'){
                    $status = 0;
                    $where['insurant.status'] =  ['not in',0];
                }
                dd($where);
                $all = 1;
                $this->assign('name',$name);
                $this->assign('status',$status);
            }
        }else{
            $all = 0;
        }
        $this->assign('all',$all);
        if(isset($this->request['policy_num']) && !empty($this->request['policy_num'])){
            $where['insurant.policy_num'] =['like','%'.$this->request['policy_num'].'%'] ;
            $this->assign('num',$this->request['policy_num']);
        }
        $res = Db::table('insurant')->where($where)->join('policy p','p.p_id=insurant.p_id','LEFT')->field('p.report_num,insurant.status,p.policy_num,insurant.created_at')->select();
        foreach ($res as $k=>$v){
            $res[$k]['status_name'] = $this->remerStatus($res[$k]['status']);
        }
        return $this->fetch('insurant/history_info',['res'=>$res]);
    }


    /**
     * （详情） 1 3 4
     */
    public function no_accept()
    {
        $policy_num = $this->request['policy_num'];
        $info = Db::table('policy')->where('policy_num',$policy_num)->field('policy_num,report_num,insurance_type,p_id')->find();
        $res = Db::table('insurant')->where('p_id',$info['p_id'])->find();
        if($res['entrust_url']){
            $res['entrust_url'] = json_decode($res['entrust_url']);
        }
        $info['insurance_type'] = $this->getInsurantType($info['insurance_type']);
        $res['status_name'] = $this->remerStatus($res['status']);
        if(!empty($this->request['is_show'])){
            $show = 1;
        }else{
            $show = 2;
        }
        if($res['status'] == 3){
            $where = [
                'i_id'=>$res['i_id'],
                'status'=>1,
            ];
            $returnMsg = Db::table('return_insurant')->where($where)->select();
            $data = [];
            foreach ($returnMsg as $k=>$v){
                if(!empty($v['join_price'])){
                    $data['join_price'][] = $v['join_price'];
                }
                if(!empty($v['look_goods_info'])){
                    $data['look_goods_info'][] = $v['look_goods_info'];
                }
                if(!empty($v['bol_info'])){
                    $data['bol_info'][] = $v['bol_info'];
                }
                if(!empty($v['receive_money'])){
                    $data['receive_money'][] = $v['receive_money'];
                }
                if(!empty($v['auth_report'])){
                    $data['auth_report'][] = $v['auth_report'];
                }
            }
            $this->assign('data',$data);
        }
        $this->assign('show',$show);
        return $this->fetch('insurant/history_detail',['res'=>$res,'info'=>$info]);
    }


    /**
     * 驳回修改
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function returnEdit()
    {
        $policy_num = $this->request['policy_num'];
        $info = Db::table('policy')->where('policy_num',$policy_num)->find();
        $res = Db::table('insurant')->where('p_id',$info['p_id'])->find();
        $res['entrust_url'] = json_decode($res['entrust_url']);
        $info['insurance_type'] = $this->getInsurantType($info['insurance_type']);
        $where = [
            'i_id'=>$res['i_id'],
            'status'=>1,
        ];
        $returnMsg = Db::table('return_insurant')->where($where)->select();
        $data = [];
        foreach ($returnMsg as $k=>$v){
            if(!empty($v['join_price'])){
                $data['join_price'][] = $v['join_price'];
            }
            if(!empty($v['look_goods_info'])){
                $data['look_goods_info'][] = $v['look_goods_info'];
            }
            if(!empty($v['bol_info'])){
                $data['bol_info'][] = $v['bol_info'];
            }
            if(!empty($v['receive_money'])){
                $data['receive_money'][] = $v['receive_money'];
            }
            if(!empty($v['auth_report'])){
                $data['auth_report'][] = $v['auth_report'];
            }
        }
        $this->assign('p_id',$res['p_id']);
        return $this->fetch('insurant/return_edit',['res'=>$res,'data'=>$data]);
    }

    /**
     * 参与竞价页面修改（驳回）
     */
    public function return_price()
    {
        $p_id = $this->request['p_id'];
        $res = Db::table('insurant')->where('p_id',$p_id)->find();
        $data = Db::table('policy')->where('p_id',$p_id)->find();
        $res['policy_num'] = $data['policy_num'];
        return $this->fetch('insurant/return_price',['info'=>$res]);
    }


    /**
     * 看货信息页面修改（驳回）
     */
    public function return_goods_info()
    {
        $p_id = $this->request['p_id'];
        $res = Db::table('insurant')->where('p_id',$p_id)->find();
        $data = Db::table('policy')->where('p_id',$p_id)->find();
        $res['look_type'] = array_flip($this->type)[$res['look_type']];
        $res['policy_num'] = $data['policy_num'];
        return $this->fetch('insurant/return_goods_info',['info'=>$res]);
    }

    /**
     * 提货信息页面修改（驳回）
     */
    public function return_bol()
    {
        $p_id = $this->request['p_id'];
        $res = Db::table('insurant')->where('p_id',$p_id)->find();
        $data = Db::table('policy')->where('p_id',$p_id)->find();
        $res['policy_num'] = $data['policy_num'];
        return $this->fetch('insurant/return_bol',['info'=>$res]);
    }

    /**
     * 收款信息页面修改（驳回）
     */
    public function return_money()
    {
        $p_id = $this->request['p_id'];
        $res = Db::table('insurant')->where('p_id',$p_id)->find();
        $data = Db::table('policy')->where('p_id',$p_id)->find();
        $res['policy_num'] = $data['policy_num'];
        return $this->fetch('insurant/return_money',['info'=>$res]);
    }

    /**
     * 授权书页面修改（驳回）
     */
    public function return_report()
    {
        $p_id = $this->request['p_id'];
        $res = Db::table('insurant')->where('p_id',$p_id)->find();
        $data = Db::table('policy')->where('p_id',$p_id)->find();
        $res['entrust_url'] = json_decode($res['entrust_url']);
        $res['policy_num'] = $data['policy_num'];
        return $this->fetch('insurant/return_report',['info'=>$res]);
    }



    /**
     * 参与竞价页面
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function step_1()
    {
        $policy_num = isset($this->request['policy_num']) ? $this->request['policy_num'] : null;
        $policy = Db::table('policy')->where('policy_num',$policy_num)->find();
        $info = Db::table('insurant')->where('p_id',$policy['p_id'])->find();
        $this->assign('info',$info);
        return $this->fetch('insurant/step_1');
    }


    /**
     * 看货信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function step_2()
    {
        $i_id = isset($this->request['i_id']) ? $this->request['i_id'] : null;
        if($i_id){
            $info = Db::table('insurant')->where('i_id',$i_id)->find();
            if(!empty($info['look_type'])){
                $info['look_type'] = array_flip($this->type)[$info['look_type']];
            }
            $this->assign('info',$info);
        }
        return $this->fetch('insurant/step_2');
    }

    /**
     * 提货信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function step_3()
    {
        $i_id = isset($this->request['i_id']) ? $this->request['i_id'] : null;
        if($i_id){
            $info = Db::table('insurant')->where('i_id',$i_id)->find();
            $this->assign('info',$info);
        }
        return $this->fetch('insurant/step_3');
    }

    /**
     * 返回提货时限
     * @return \think\response\Json
     */
    public function getBolDays()
    {
        $arr = Db::table('bol_days')->field('it_name')->select();
        return json(['arr'=>$arr]);
    }

    /**
     * 收款信息
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function step_4()
    {
        $i_id = isset($this->request['i_id']) ? $this->request['i_id'] : null;
        if($i_id){
            $info = Db::table('insurant')->where('i_id',$i_id)->find();
            $this->assign('info',$info);
        }
        return $this->fetch('insurant/step_4');
    }


    /**
     * 递交委托书
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function step_5()
    {
        $i_id = isset($this->request['i_id']) ? $this->request['i_id'] : null;
        if($i_id){
            $info = Db::table('insurant')->where('i_id',$i_id)->find();
            if($info['entrust_url']){
                $info['entrust_url'] = json_decode($info['entrust_url']);
            }
            $data = Db::table('policy')->where('p_id',$info['p_id'])->find();
            $this->assign('info',$info);
            $this->assign('data',$data);
        }

        return $this->fetch('insurant/step_5');
    }


    /**
     * 打印委托书列表
     * @return \think\response\View
     */
    public function print_report()
    {
        $info = Db::table('insurant')
            ->where('insurant.status',4)
            ->join('policy','policy.p_id=insurant.p_id','left')
            ->join('user_info','policy.remer_id=user_info.ui_id','left')
            ->field('policy.policy_num,user_info.username')
            ->select();
        foreach ($info as $k=>$v){
            $where = [
                'type'=>1,
                'policy_num'=>$v['policy_num']
            ];
            $res = Db::table('download_info')->where($where)->find();
            if($res){
                $info[$k]['is_show'] = 1;
            }else{
                $info[$k]['is_show'] = 2;
            }
        }
        $this->assign('info',$info);
        return view();
    }

    public function report_detail()
    {
        return view();
    }



    /**
     * 下载提货单
     * @return mixed
     */
    public function download_bol()
    {
        $res = Db::table('insurant')
            ->where('status',4)
            ->join('policy','policy.p_id=insurant.p_id','left')
            ->field('policy.policy_num,policy.created_at,insurant.admin_bol_upload')
            ->select();
        $this->assign('res',$res);
        return $this->fetch('insurant/download_bol');
    }







    /**
     * 上传提货单
     * @return mixed
     */
    public function upload_bol()
    {
        $res = Db::table('insurant')
            ->where('status',4)
            ->join('policy','policy.p_id=insurant.p_id','left')
            ->field('policy.policy_num,policy.created_at,insurant.admin_bol_upload,insurant.i_id')
            ->select();
        foreach ($res as $k=>$v){
            $where = [
                'type'=>3,
                'policy_num'=>$v['policy_num']
            ];
            $data = Db::table('download_info')->where($where)->find();
            if($data){
                $res[$k]['is_show'] = 1;
            }else{
                $res[$k]['is_show'] = 2;
            }
        }
        $this->assign('res',$res);
        return $this->fetch('insurant/upload_bol');
    }

    /**
     * 保存上传的提货单
     * @return \think\response\Json
     */
    public function save_upload()
    {
        try{
            Db::startTrans();
            $data = [
                'type'=>$this->request['type'],
                'created_at'=>time(),
                'policy_num'=>$this->request['policy_num'],
            ];
            Db::table('insurant')->where('i_id',$this->request['i_id'])->update(['admin_bol_upload'=>$this->request['url']]);
            Db::table('download_info')->insert($data);
            Db::commit();
            return json(['code'=>200,'msg'=> 'success']);
        }catch(Exception $e){
            Db::rollback();
            return json(['code'=>400,'msg'=> 'fail']);
        }
    }



    public function return_save()
    {
        $i_id = $this->request['i_id'];
        $data = [
            'status'=>1,
        ];
        try{
            Db::startTrans();
            $where = [
                'auth_id'=>$this->uid,
                'form_table'=>'user_info',
                'i_id'=>$i_id
            ];
            Db::table('return_insurant')->where($where)->update($data);
            $conidtion = [
                'i_id'=>$i_id,
                'status'=>1,
            ];
            $res = Db::table('return_insurant')->where($conidtion)->select();
            if(!$res){
                Db::table('insurant')->where('i_id',$i_id)->update(['status'=>3]);
            }
            Db::commit();
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }catch(Exception $e){
            Db::rollback();
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'']);
        }
    }



    /****************************************************************************************************/
    /**
     * 被保险人--核对信息
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCheckInfo()
    {
        $UserInfoModel = new UserInfoModel();
        $user_id = $this->request['ui_id'] = 1;
        $info = $UserInfoModel->getPolicyByUserId($user_id);
        return $this->response->json(['data'=>$info,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }

    /**
     * 被保险人-- 资料未确认
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getGoodInfo()
    {
        $policy_id = $this->request['p_id'];
        $PolicyModel = new PolicyModel();
        $info = $PolicyModel->getPolicyInfo($policy_id);
        return $this->response->json(['data'=>$info,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }

    /**
     * 被保险人--下一步--获取信息
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNextBol()
    {
        $i_id = isset($this->request['i_id']) ? $this->request['i_id'] : '';
        $p_id = isset($this->request['p_id']) ? $this->request['p_id'] : '';
        $where = [
            'i_id'=>$i_id,
            'p_id'=>$p_id,
        ];
        $res = Db::table('insurant')->where($where)->find();
        if($res){
            return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }else{
            return $this->response->json(['data'=>[],'code'=>self::SUCCESS_CODE,'msg'=>'fail']);
        }
    }




    /**
     * 被保险人---下一步--保存
     */
    public function saveBolInfo()
    {
        $id = isset($this->request['i_id']) ? $this->request['i_id'] : '';
        if(!empty($this->request['is_join'])){
            $data['is_join'] = $this->request['is_join'];
        }
        if(!empty($this->request['is_promise'])){
            $data['is_promise'] = $this->request['is_promise'];
        }
        if(!empty($this->request['back_price'])){
            $data['back_price'] = $this->request['back_price'];
        }
        if(!empty($this->request['address_detail'])){
            $data['address_detail'] = $this->request['address_detail'];
        }
        if(!empty($this->request['linkman'])){
            $data['linkman'] = $this->request['linkman'];
        }
        if(!empty($this->request['phone'])){
            $data['phone'] = $this->request['phone'];
        }
        if(!empty($this->request['second_phone'])){
            $data['second_phone'] = $this->request['second_phone'];
        }
        if(!empty($this->request['is_pay'])){
            $data['is_pay'] = $this->request['is_pay'];
        }
        if(!empty($this->request['pay_result'])){
            $data['pay_result'] = $this->request['pay_result'];
        }
        if(!empty($this->request['bol_info'])){
            $data['bol_info'] = $this->request['bol_info'];
        }
        if(!empty($this->request['bol_address_detail'])){
            $data['bol_address_detail'] = $this->request['bol_address_detail'];
        }
        if(!empty($this->request['bol_linkman'])){
            $data['bol_linkman'] = $this->request['bol_linkman'];
        }
        if(!empty($this->request['bol_phone'])){
            $data['bol_phone'] = $this->request['bol_phone'];
        }
        if(!empty($this->request['pay_name'])){
            $data['pay_name'] = $this->request['pay_name'];
        }
        if(!empty($this->request['card_num'])){
            $data['card_num'] = $this->request['card_num'];
        }
        if(!empty($this->request['tax_num'])){
            $data['tax_num'] = $this->request['tax_num'];
        }
        if(!empty($this->request['company_address'])){
            $data['company_address'] = $this->request['company_address'];
        }
        if(!empty($this->request['entrust_url'])){
            $data['entrust_url'] = json_encode($this->request['entrust_url']);
        }
        if(!empty($this->request['bol_url'])){
            $data['bol_url'] = $this->request['bol_url'];
        }
        if(!empty($this->request['now_status'])){
            $data['now_status'] = $this->request['now_status'];
        }
        if(!empty($this->request['address'])){
            $arr = explode(" ",$this->request['address']);
            if(count($arr) == 2){
                list($city,$town) = $arr;
                $data['city'] = $city;
                $data['town'] = $town;
            }
            if(count($arr) == 3){
                list($province,$city,$town) = $arr;
                $data['province'] = $province;
                $data['city'] = $city;
                $data['town'] = $town;
            }
        }
        if(!empty($this->request['bol_address'])){
            $arr = explode(" ",$this->request['bol_address']);
            if(count($arr) == 2){
                list($city,$town) = $arr;
                $data['bol_city'] = $city;
                $data['bol_town'] = $town;
            }
            if(count($arr) == 3){
                list($province,$city,$town) = $arr;
                $data['bol_province'] = $province;
                $data['bol_city'] = $city;
                $data['bol_town'] = $town;
            }
        }
        if(!empty($this->request['bol_days'])){
            $patterns = '/\d+/';
            preg_match_all($patterns,$this->request['bol_days'],$d);//
            $data['bol_days'] = $d[0][0];
        }
        if(!empty($this->request['look_type'])){
            $data['look_type'] = $this->type[$this->request['look_type']];
        }
        if($data['now_status'] == 5){
            $data['status'] = 2;
        }
        $data['update_at'] = date('Y-m-d H:i:s',time());
        $InsurantModel = new InsurantModel();
        $res = $InsurantModel->where('i_id',$id)->update($data);
        if($res){
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }else{
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'fail']);
        }
    }


    /**
     * 根据用户id 获取保单信息
     */
    public function getPolicyId()
    {
        $info = Db::table('policy')->where('ui_id',$this->uid)->find();
        return $info['p_id'];
    }


    /**
     * 获取历史列表
     */
    public function getHistoryInfo()
    {
        $ui_id = 1;
        $where['insurant.ui_id'] = $ui_id;
        if(isset($this->request['status']) && !empty($this->request['status'])){
            $where['insurant.status'] = $this->request['status'];
        }
        $res = Db::table('insurant')->join('policy p','p.p_id=insurant.p_id','LEFT')->field('p.report_num,p.policy_status,insurant.created_at')->select();
        return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }

    /**
     * 历史详情（所有状态）
     */
    public function historyInfoById()
    {
        $i_id = $this->request['i_id'];
        $data['res'] = Db::table('insurant')->where('i_id',$i_id)->find();
        if($i_id == 3){
            $where = [
                'i_id'=>$i_id,
                'status'=>1,
            ];
            $returnMsg = Db::table('return_insurant')->where($where)->select();
            $data['return_msg'] = $returnMsg;
        }
        return $this->response->json(['data'=>$data,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }


    /**
     * 打印委托书
     */
    public function authorReport()
    {
        $ui_id = $this->request['ui_id'];
        $res = Db::table('policy')->where('remer_id',$ui_id)->find();
        return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }



}